﻿Imports System.Globalization

Partial Public Class Holiday
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "HOLIDAY MANAGEMENT "
            ddlMonth.Items.Clear()
            For i As Integer = 1 To 12
                With ddlMonth
                    .Items.Insert(i - 1, GetNameMonth(i.ToString("00")))
                    .Items(i - 1).Value = i.ToString("00")
                End With
            Next
            ddlYear.Items.Clear()
            Dim count As Integer = -3
            For i As Integer = 1 To 5
                With ddlYear
                    .Items.Insert(i - 1, Date.Now.AddYears(i + count).ToString("yyyy", New CultureInfo("en-US")))
                    .Items(i - 1).Value = Date.Now.AddYears(i + count).ToString("yyyy", New CultureInfo("en-US"))
                End With
            Next
            If Not (ddlMonth.Items.FindByValue(Date.Now.ToString("MM")) Is Nothing) Then
                ddlMonth.SelectedValue = Date.Now.ToString("MM")
            End If
            If Not (ddlYear.Items.FindByValue(Date.Now.ToString("yyyy", New CultureInfo("en-US"))) Is Nothing) Then
                ddlYear.SelectedValue = Date.Now.ToString("yyyy", New CultureInfo("en-US"))
            End If
            ddlDay.Items.Clear()
            For i As Integer = 1 To Date.DaysInMonth(CInt(ddlYear.SelectedValue), CInt(ddlMonth.SelectedValue))
                With ddlDay
                    .Items.Insert(i - 1, i)
                    .Items(i - 1).Value = i
                End With
            Next
            If Not (ddlDay.Items.FindByValue(CInt(Date.Now.ToString("dd")).ToString()) Is Nothing) Then
                ddlDay.SelectedValue = CInt(Date.Now.ToString("dd")).ToString()
            End If
            LoadData("", "", "")
            If (Request.QueryString("P1") & "" = "Y") Then
                Session("EditMaster") = "Y"
            Else
                Session("EditMaster") = Nothing
            End If
            If (Request.QueryString("P2") & "" <> "Y") Then
                btnNewFacility.Visible = False
            End If
            If (Request.QueryString("P3") & "" <> "Y") Then
                gridFacility.Columns(4).Visible = False
            End If
        End If
    End Sub
    Private Sub LoadData(ByVal Day As String, ByVal Month As String, ByVal Year As String)
        ViewState("Holiday") = clsGet.Get_HolidayALl(Day, Month, Year)
        gridFacility.PageIndex = 0
        gridFacility.DataSource = ViewState("Holiday")
        gridFacility.DataBind()
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridFacility.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridFacility.PageIndex = ddlPage.SelectedIndex
        gridFacility.DataSource = ViewState("Holiday")
        gridFacility.DataBind()
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridFacility.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridFacility.PageIndex = 0
            Case "prev"
                If (gridFacility.PageIndex <> 0) Then
                    gridFacility.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridFacility.PageIndex <> gridFacility.PageCount) Then
                    gridFacility.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridFacility.PageIndex = gridFacility.PageCount
        End Select
        gridFacility.DataSource = ViewState("Holiday")
        gridFacility.DataBind()
    End Sub

    Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        LoadData("", "", "")
    End Sub

    Private Sub gridFacility_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridFacility.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridFacility.PageIndex <> 0) Then
            Index = Index Mod (gridFacility.PageIndex * 10)
        End If
        If e.CommandName = "cmdedit" Then
            Session("Holiday") = gridFacility.DataKeys(Index).Values("Day") & "$" & gridFacility.DataKeys(Index).Values("Month") & "$" & gridFacility.DataKeys(Index).Values("Year")
            ModalPopupExtender1.Show()
        ElseIf e.CommandName = "cmddelete" Then
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            tran = clsDB.BeginTran(conn)
            If (clsFc.DeleteHoliday(gridFacility.DataKeys(Index).Values("Day"), gridFacility.DataKeys(Index).Values("Month"), gridFacility.DataKeys(Index).Values("Year"), conn, tran) < 1) Then
                condition = False
            End If
            If (condition) Then
                clsDB.CommitTran(tran)
                LoadData("", "", "")
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ลบข้อมูลเรียบร้อย!!');", True)
            Else
                clsDB.RollBackTran(tran)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการลบข้อมูล !!');", True)
            End If
            conn.Close()
            Application.UnLock()
            conn.Dispose()
        End If
    End Sub

    Private Sub gridFacility_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridFacility.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            e.Row.Cells(1).Text = GetNameMonth(gridFacility.DataKeys(e.Row.RowIndex).Values("Month") & "")
        ElseIf (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridFacility.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridFacility.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridFacility.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridFacility.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadData(ddlDay.SelectedValue, ddlMonth.SelectedValue, ddlYear.SelectedValue)
    End Sub

    Private Sub btnNewFacility_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewFacility.Click
        Session("Holiday") = Nothing
        ModalPopupExtender1.Show()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Function GetNameMonth(ByVal Month As String) As String
        Try
            Select Case (CInt(Month).ToString("00"))
                Case "01"
                    Return "January"
                Case "02"
                    Return "February"
                Case "03"
                    Return "March"
                Case "04"
                    Return "April"
                Case "05"
                    Return "May"
                Case "06"
                    Return "June"
                Case "07"
                    Return "July"
                Case "08"
                    Return "August"
                Case "09"
                    Return "September"
                Case "10"
                    Return "October"
                Case "11"
                    Return "November"
                Case "12"
                    Return "December"
                Case Else
                    Return ""
            End Select
        Catch ex As Exception
            Return Month
        End Try

    End Function

    Private Sub ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged
        ddlDay.Items.Clear()
        For i As Integer = 1 To Date.DaysInMonth(CInt(ddlYear.SelectedValue), CInt(ddlMonth.SelectedValue))
            With ddlDay
                .Items.Insert(i - 1, i)
                .Items(i - 1).Value = i
            End With
        Next
    End Sub

    Private Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChanged
        ddlDay.Items.Clear()
        For i As Integer = 1 To Date.DaysInMonth(CInt(ddlYear.SelectedValue), CInt(ddlMonth.SelectedValue))
            With ddlDay
                .Items.Insert(i - 1, i)
                .Items(i - 1).Value = i
            End With
        Next
    End Sub
End Class